package org.medicalinstrument.wechatManage.servlet;


import org.dom4j.DocumentException;
import org.medicalinstrument.wechatManage.utils.MessageUtil;
import org.medicalinstrument.wechatManage.utils.CheckUtil;
import org.medicalinstrument.wechatManage.utils.Message;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * @Author: kongHuan
 * @Date: 2019/3/9 8:38
 * @Description:
 */
public class WechatServlet extends HttpServlet {
    //打印日志
    //static Logger logger = LoggerFactory.getLogger(WechatServlet.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("-----开始校验签名-----");

        /**
         * 接收微信服务器发送请求时传递过来的参数
         */
        String signature = req.getParameter("signature");

        String timestamp = req.getParameter("timestamp");

        String nonce = req.getParameter("nonce");

        String echostr = req.getParameter("echostr");



        PrintWriter out = resp.getWriter();

        if(CheckUtil.checkSignature(signature, timestamp, nonce)){

//如果校验成功，将得到的随机字符串原路返回

            out.print(echostr);

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        resp.setCharacterEncoding("UTF-8");

        PrintWriter out = resp.getWriter();

        String str = null;

        try {

//将request请求，传到Message工具类的转换方法中，返回接收到的Map对象

            Map<String, String> map = Message.xmlToMap(req);

//从集合中，获取XML各个节点的内容

            String ToUserName = map.get("ToUserName");

            String FromUserName = map.get("FromUserName");

            String CreateTime = map.get("CreateTime");

            String MsgType = map.get("MsgType");

            String Content = map.get("Content");

            String MsgId = map.get("MsgId");

            /*if(MsgType.equals("text")){//判断消息类型是否是文本消息(text)

                TextMessage message = new TextMessage();

                message.setFromUserName(ToUserName);//原来【接收消息用户】变为回复时【发送消息用户】

                message.setToUserName(FromUserName);

                message.setMsgType("text");

                message.setCreateTime(new Date().getTime());//创建当前时间为消息时间

                message.setContent("您好，"+FromUserName+"\n我是："+ToUserName

                        +"\n您发送的消息类型为："+MsgType+"\n您发送的时间为"+CreateTime

                        +"\n我回复的时间为："+message.getCreateTime()+"您发送的内容是"+Content);

                str = Message.objectToXml(message); //调用Message工具类，将对象转为XML字符串

            }*/
            String message = null;

            if (MsgType.equals(MessageUtil.MESSAGE_TEXT)) {//判断是否为文本消息类型

                if (Content.equals("1")) {

                    message = MessageUtil.initText(ToUserName, FromUserName,

                            "对啊！我也是这么觉得！");

                } else if(Content.equals("2")) {

                    message = MessageUtil.initText(ToUserName, FromUserName,

                            "好可怜啊！你年级轻轻地就瞎了！");
                }else if(Content.equals("3")){

                    message = MessageUtil.initNewsMessage(ToUserName, FromUserName);

                }else if(Content.equals("?") || Content.equals("？")){

                    message = MessageUtil.initText(ToUserName, FromUserName,

                            MessageUtil.menuText());

                } else {

                    message = MessageUtil.initText(ToUserName, FromUserName,

                            "没让你选的就别瞎嘚瑟！！！");

                }



            }else if(MsgType.equals(MessageUtil.MESSAGE_EVENT)){//判断是否为事件类型

//从集合中，或许是哪一种事件传入

                String eventType = map.get("Event");

//关注事件

                if (eventType.equals(MessageUtil.MESSAGE_SUBSCRIBE)) {

                    message = MessageUtil.initText(ToUserName, FromUserName,

                            MessageUtil.menuText());

                }

            }



            System.out.println(message);

            out.print(message);

        } catch (DocumentException e) {

            e.printStackTrace();

        }

        out.print(str); //返回转换后的XML字符串

        out.close();

    }


}
